import com.github.kwhat.jnativehook.GlobalScreen;
import com.github.kwhat.jnativehook.NativeHookException;
import constant.Context;
import listen.KeyboardListener;
import lombok.extern.slf4j.Slf4j;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.io.IOException;
import java.net.URL;

/**
 * @Author: gaoxu11
 * @Date: 2022/5/13 2:24 AM
 * @Description:GImage.Main
 */
@Slf4j
public class Main {
    public static void main(String[] args) {
        try {
            GlobalScreen.registerNativeHook();
            log.info("当前用户使用的系统为:{}", System.getProperty("os.name"));
            new Main();
            log.info("全局按键捕捉已启动");
        } catch (NativeHookException | IOException ex) {
            log.error("There was a problem registering the native hook. error:{}", ex.getMessage());
            System.exit(1);
        }

        GlobalScreen.addNativeKeyListener(new KeyboardListener());
    }

    public Main() throws IOException {
        init();
    }

    public void init() throws IOException {
        this.setTray();
    }

    public void setTray() throws IOException {

        if (SystemTray.isSupported()) {//判断当前平台是否支持托盘功能

            //创建托盘实例

            SystemTray tray = SystemTray.getSystemTray();

            //创建托盘图标：1.显示图标Image 2.停留提示text 3.弹出菜单popupMenu 4.创建托盘图标实例

            //1.创建Image图像

            URL resource = this.getClass().getResource("/icon.png");//获取托盘图标

            ImageIcon icon = new ImageIcon(resource);

            //2.停留提示text

            String text = "GA6图床";

            //3.弹出菜单popupMenu

            PopupMenu popMenu = new PopupMenu();

            popMenu.setFont(new Font("宋体", Font.ROMAN_BASELINE, 12));

            MenuItem itmOpen = new MenuItem("Menu");
            itmOpen.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    Show();
                }
            });
            Menu format = new Menu("Model");
            MenuItem Url = new MenuItem("Url");
            MenuItem Markdown = new MenuItem("Markdown");

            format.add(Url);
            format.add(Markdown);


            Markdown.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    changeModel(true);
                }
            });

            Url.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    changeModel(false);
                }
            });

            MenuItem itmExit = new MenuItem("Exit");
            itmExit.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    Exit();
                }

            });
            popMenu.add("GA666666");
            popMenu.addSeparator();
            popMenu.add(itmOpen);

            popMenu.add(format);

            popMenu.add(itmExit);

            //创建托盘图标

            TrayIcon trayIcon = new TrayIcon(icon.getImage(), text, popMenu);

            trayIcon.setImageAutoSize(true);

            //将托盘图标加到托盘上

            try {

                tray.add(trayIcon);

            } catch (AWTException e1) {

                e1.printStackTrace();

            }

        }

    }


    public void changeModel(boolean mark) {
        Context.MARKDOWN = mark;
        log.info("Main changeModel [链接模式修改] Model:{}", Context.MARKDOWN ? "Markdown" : "Url");
    }

    public void Show() {

    }

    public void Exit() {
        System.exit(0);
    }

}
